/**
 * @param {number[][]} values
 * @return {number}
 * 取每个队列末尾，比最小，移动指针
 *
 */
const maxSpending = function(values: number[][]): number {
    const m = values.length
    const n = values[0].length
    let result = 0
    const resultArr: number[] = []
    const lengthArr: number[] = new Array(m).fill(n-1)
    for (let i=1; i <= m*n; i++) {
        const lastArr: number[] = []
        values.forEach((item: number[], index: number) => {
            // console.log(item, index, lengthArr[index], item[lengthArr[index]])
            if (lengthArr[index] >= 0) lastArr.push(item[lengthArr[index]]) 
                else lastArr.push(-1) 
        })
        const min = Math.min(...lastArr.filter(item => item >= 0))
        const index = lastArr.indexOf(min)
        lengthArr[index]--
        values[index]?.pop()
        resultArr.push(min)
        result += min*i
        console.table([['lastArr', lastArr],['resultArr', JSON.stringify(resultArr)],['result', result]])
    }
    return result
};


const values1 = [[8,5,2],[6,4,1],[9,7,3]]
const values2 = [[10,8,6,4,2],[9,7,5,3,2]]
console.log(maxSpending(values2))